package com.lanou3g.musicenjoy.download;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.RemoteViews;

import com.lanou3g.musicenjoy.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NotificationBean extends Notification {
    private Context mContext;

    public NotificationBean(Context context, int icon, CharSequence tickerText, long when, final String picUrl, String title) {
        super(icon, tickerText, when);
        this.mContext = context;
        this.flags = Notification.FLAG_AUTO_CANCEL; // |=
//		this.flags = Notification.FLAG_ONGOING_EVENT;

        RemoteViews mRemoteView = new RemoteViews(mContext.getPackageName(), R.layout.remote_view);
        this.contentView = mRemoteView;

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(picUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    if (connection.getResponseCode() == 200) {
                        InputStream is = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        contentView.setImageViewBitmap(R.id.down_image, bitmap);
                        Log.i("down", "设置网络图片");
//                        if (bitmap != null) {
//                            contentView.setImageViewBitmap(R.id.down_image, bitmap);
//                            Log.i("down", "设置网络图片");
//                        } else {
//                            contentView.setImageViewResource(R.id.down_image, R.mipmap.music);
//                            Log.i("down", "设置本地图片");
//                        }
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread mThread = new Thread(runnable);
        if (picUrl != null && picUrl.length() > 0) {
            mThread.start();
            Log.i("bbbbb", "开启线程设置网络图片");
        } else {
            contentView.setImageViewResource(R.id.down_image, R.mipmap.music);
            Log.i("bbbbb", "设置本地图片");
        }
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    URL url = new URL(picUrl);
//                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                    if (connection.getResponseCode() == 200) {
//                        InputStream is = connection.getInputStream();
//                        Bitmap bitmap = BitmapFactory.decodeStream(is);
//                        if (bitmap != null) {
//                            contentView.setImageViewBitmap(R.id.down_image, bitmap);
//                            Log.i("down", "设置网络图片");
//                        } else {
//                            contentView.setImageViewResource(R.id.down_image, R.mipmap.music);
//                            Log.i("down", "设置本地图片");
//                        }
//                    }
//                } catch (MalformedURLException e) {
//                    e.printStackTrace();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();

//        if (bitmap != null) {
//            contentView.setImageViewBitmap(R.id.down_image, bitmap);
//            Log.i("tttttt", "设置网络图片");
//        } else {
//            Log.i("tttttt", "设置本地图片");
//            contentView.setImageViewResource(R.id.down_image, R.mipmap.music);
//
//        }

        contentView.setTextViewText(R.id.load_song_name, title);
    }

}
